package org.jeecg.modules.scm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.scm.dto.ItemSearchDto;
import org.jeecg.modules.scm.entity.ScmCusInfo;
import org.jeecg.modules.scm.entity.ScmFinaContract;
import org.jeecg.modules.scm.entity.ScmItem;
import org.jeecg.modules.scm.service.IScmCusInfoService;
import org.jeecg.modules.scm.service.IScmFinaContractService;
import org.jeecg.modules.scm.service.IScmItemService;
import org.jeecg.modules.scm.service.IScmSupplyChainService;
import org.jeecg.modules.scm.vo.CusChainInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目管理控制器
 * @Author: zzq
 * @Date: 2021-10-21
 * @Version: V1.0
 */
@Api(tags = "项目管理")
@RestController
@RequestMapping("/scm/item")
@Slf4j
public class ScmItemController extends JeecgController<ScmItem, IScmItemService> {
    @Autowired
    private IScmItemService scmItemService;

    @Autowired
    private IScmCusInfoService scmCusInfoService;

    @Autowired
    private IScmSupplyChainService scmSupplyChainService;

    @Autowired
    private IScmFinaContractService scmFinaContractService;

    /**
     * 分页列表查询
     *
     * @param scmItem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @ApiOperation(value = "scm_item-分页列表查询", notes = "scm_item-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ScmItem scmItem,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<ScmItem> queryWrapper = QueryGenerator.initQueryWrapper(scmItem, req.getParameterMap());
        Page<ScmItem> page = new Page<ScmItem>(pageNo, pageSize);
        IPage<ScmItem> pageList = scmItemService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param scmItem
     * @return
     */
    @AutoLog(value = "scm_item-添加")
    @ApiOperation(value = "scm_item-添加", notes = "scm_item-添加")
    @PostMapping(value = "/add")
    public Result<Integer> add(@RequestBody ScmItem scmItem) {
        scmItemService.check(scmItem);
        scmItemService.save(scmItem);
        return Result.OK("添加成功！", scmItem.getItemId());
    }

    /**
     * 编辑
     *
     * @param scmItem
     * @return
     */
    @AutoLog(value = "scm_item-编辑")
    @ApiOperation(value = "scm_item-编辑", notes = "scm_item-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ScmItem scmItem) {
        scmItemService.updateById(scmItem);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_item-通过id删除")
    @ApiOperation(value = "scm_item-通过id删除", notes = "scm_item-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        scmItemService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "scm_item-批量删除")
    @ApiOperation(value = "scm_item-批量删除", notes = "scm_item-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.scmItemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "scm_item-通过id查询")
    @ApiOperation(value = "scm_item-通过id查询", notes = "scm_item-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) Integer id) {
        ScmItem scmItem = scmItemService.getById(id);
        if (scmItem == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(scmItem);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param scmItem
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScmItem scmItem) {
        return super.exportXls(request, scmItem, ScmItem.class, "scm_item");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScmItem.class);
    }


    //************************************************************************

    @ApiOperation(value = "查看项目融资", notes = "根据融资合同表查询项目融资列表（按照放款日期降序排列）")
    @GetMapping("/queryFinsByItemId")
    public Result<?> queryFinByItemId(@RequestParam Integer itemId) {
        List<ScmFinaContract> list = scmFinaContractService.getListByItemId(itemId);
        return Result.OK(list);
    }

    @ApiOperation(value = "根据项目查看核心客户信息", notes = "返回核心客户信息")
    @GetMapping("/queryCusInfoByItemId")
    public Result<ScmCusInfo> queryCusInfoByItemId(@RequestParam Integer itemId) {
        // TODO 可以不用调用这个接口
        Integer cusId = scmItemService.getCusId(itemId);
        ScmCusInfo cusInfo = scmCusInfoService.getById(cusId);
        if (null == cusInfo) {
            return Result.OK("无数据", new ScmCusInfo());
        }
        return Result.OK(cusInfo);
    }


    @ApiOperation(value = "根据项目查看客户列表", notes = "返回项目供应链关联的客户列表")
    @GetMapping("/queryCusChainInfo")
    public Result<List<CusChainInfoVo>> queryCusChainInfo(@RequestParam(value = "id", required = true) Integer id) {

        List<CusChainInfoVo> cusChainInfoVos = scmSupplyChainService.getCusChainInfo(id);
        return Result.OK(cusChainInfoVos);
    }

    @ApiOperation(value = "更变项目状态", notes = "status 1、正常 2、终结")
    @PutMapping("/updateStatusById")
    public Result<?> updateStatusById(Integer item, int status) {
        scmItemService.updateStatus(item, status);
        return Result.OK("更改成功");
    }

    /**
     * 根据项目名称模糊查询项目名称和项目ID
     *
     * @param key 项目名称关键字
     * @return
     */
    @ApiOperation(value = "模糊查询客户名称列表", notes = "返回核心企业名称对应的客户号列表")
    @GetMapping("/likeCusName")
    public Result<List<Map<String, Object>>> searchCoreEnterprise(@RequestParam(value = "key", required = true) String key) {
        // 模糊查询客户名称
        List<Map<String, Object>> cusNames = scmCusInfoService.likeName(key);
        return Result.OK(cusNames);
    }

    @ApiOperation(value = "模糊查询项目名称列表", notes = "返回项目名称对应的项目列表")
    @GetMapping("/likeItemName")
    public Result<List<Map<String, Object>>> searchItemListByName(@RequestParam(value = "key", required = true) String key) {
        // 模糊查询客户名称
        List<Map<String, Object>> maps = scmItemService.likeName(key);
        return Result.OK(maps);
    }


    @ApiOperation(value = "搜索项目列表-可用高级查询代替", notes = "根据项目名称、核心企业名称、建立日期组合条件查询")
    @PostMapping("/searchItemList")
    public Result<Page<ScmItem>> searchItemList(@RequestBody ItemSearchDto searchDto) {
        Page<ScmItem> page = scmItemService.searchItemList(searchDto);
        return Result.OK(page);
    }

    @ApiOperation(value = "核心企业项目列表", notes = "获取核心企业下的项目")
    @GetMapping("/getCoreCusItem")
    public Result<?> getCoreCusItem(@RequestParam(value = "core_cus_name", required = true) String coreCusName) {
        List<String> list = scmItemService.getItemByCusName(coreCusName);
        if(list==null){
            Result.OK("未查询到数据");
        }
        return Result.OK(list);
    }


    @ApiOperation(value = "上传附件", notes = "上传附件")
    @PostMapping("/uploadFile")
    public Result<List<String>> uploadFile(MultipartFile file, Integer itemId) {
        String paths = scmItemService.uploadAccessory(file, itemId);
        return Result.OK(Arrays.asList(paths.split(",")));
    }

    @ApiOperation(value = "查询某年客户项目", notes = "查询某年客户项目")
    @PostMapping("/getItemNum")
    public Result<?> getItemNum(@RequestParam(value = "cusName") String cusName,
                                @RequestParam(value = "year") Integer year
    ){
        Map<String, Object> itemNum = scmItemService.getItemNum(cusName, year);
        return Result.OK(itemNum);
    }



}
